package local;

import java.util.HashMap;

/**
 * ThreadLocal一定保存的是key(线程)-value(一个量)
 * @author lcx
 *
 */
public class TreadLocalTest {

    static ThreadLocal<HashMap> localMap = new ThreadLocal<HashMap>(){
        @Override
        protected HashMap initialValue() {
            System.out.println(Thread.currentThread().getName()+"initialValue");
            return new HashMap();
        }
    };
    
    public static class T1 implements Runnable{
        int id;
        public T1(int id){
            this.id = id;
        }
        public void run() {
            System.out.println(Thread.currentThread().getName()+":start");
            HashMap map = localMap.get();
            for(int i=0;i<10;i++){
                map.put(i, i+id*100);
//            	localMap.set(value);
                try{
                    Thread.sleep(100);
                }catch(Exception ex){
                }
            }
            localMap.set(map);
            System.out.println(Thread.currentThread().getName()+':'+localMap.get());
        }
    }
    /**
     * Main
     * @param args
     */
    public static void main(String[] args){
        Thread[] runs = new Thread[3];
        for(int i=0;i<runs.length;i++){
            runs[i]=new Thread(new T1(i));
        }
        
        for(int i=0;i<runs.length;i++){
            runs[i].start();
        }
    }

}
